package com.wanna.common.mongo;


import com.mongodb.client.MongoIterable;
import org.bson.Document;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * mongodb 常用操作
 *
 * @author wanna
 * @time 2018-6-13
 */
public class Mongo {

    public static Document doc() {
        return new Document();
    }

    public static Document doc(Document doc) {
        return new Document(doc);
    }

    public static Document doc(String key, Object value) {
        return doc().append(key, value);
    }

    public static Document projection() {
        return projection("_id", 0);
    }

    public static Document projection(String key, int val) {
        return doc(key, val);
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////

    public static Document appendTimestamp(long startTime, long endTime) {
        return gte(startTime).append("$lte", endTime);
    }

    public static Document appendDate(long startDate, long endDate) {
        return gte(new Date(startDate)).append("$lte", new Date(endDate));
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////


    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////

    public static Document match(Object val) {
        return doc("$match", val);
    }

    public static Document group(Object val) {
        return doc("$group", val);
    }

    public static Document unwind(String val) {
        return doc("$unwind", val);
    }

    public static Document regex(Object val) {
        return doc("$regex", val);
    }

    public static Document in(List<? extends Object> val) {
        return doc("$in", val);
    }

    public static Document or(List<? extends Object> val) {
        return doc("$or", val);
    }


    public static Document exists(boolean exists) {
        return doc("$exists", exists);
    }

    public static Document set(String key, Object value) {
        return set(Mongo.doc(key, value));
    }

    public static Document set(Object value) {
        return doc("$set", value);
    }

    public static Document rename(String origin, String newKey) {
        return doc("$rename", doc(origin, newKey));
    }

    public static Document eq(Object val) {
        return doc("$eq", val);
    }

    public static Document ne(Object val) {
        return doc("$ne", val);
    }

    public static Document sum(Object val) {
        return doc("$sum", val);
    }

    public static Document gte(Object val) {
        return doc("$gte", val);
    }

    public static Document lte(Object val) {
        return doc("$lte", val);
    }

    public static Document gt(Object val) {
        return doc("$gt", val);
    }

    public static Document lt(Object val) {
        return doc("$lt", val);
    }

    public static Document addToSet(Object val) {
        return doc("$addToSet", val);
    }

    public static Document push(Object val) {
        return doc("$push", val);
    }

    public static Document project(Object val) {
        return doc("$project", val);
    }

    public static Document lookup(Object val) {
        return doc("$lookup", val);
    }

    public static Document sort(String key, int order) {
        return doc(key, order);
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /**
     * Iterable 转成List
     *
     * @param iterable iterable
     * @param <T>      T 泛型
     * @return List
     * @link https://www.programcreek.com/java-api-examples/?code=dd00f/ibm-performance-monitor/ibm-performance-monitor-master/com.ibm.profiler.mongo/src/test/java/com/ibm/profiler/mongo/ProfiledMongoClientTest.java
     */
    public static <T> List<T> docToList(MongoIterable<T> iterable) {
        List<T> retVal = new ArrayList<>();
        return iterable.into(retVal);
    }

    public static <T> void docToSet(MongoIterable<T> iterable, Set<T> set) {
        iterable.into(set);
    }
}

